﻿using Lis.IDAL;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lis.KISSFRSTDAL
{
    public class UserInfoDal : BaseDal<UserInfo>, IUserInfoDal
    {
        #region 01，根据读者条码查询读者信息+UserInfo GetById(string id)
        /// <summary>
        /// 根据读者条码查询读者信息
        /// </summary>
        /// <param name="id">读者卡号</param>
        /// <returns></returns>
        public UserInfo GetById(string id)
        {
            return DB.QueryEntity<UserInfo>("Lis.User.Select.OneById", new { DZTM = id });
        } 
        #endregion

        #region 02，分页查询+override IQueryable<UserInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, S>> orderByLambda)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="total"></param>
        /// <param name="isAsc"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderByLambda"></param>
        /// <returns></returns>
        public override IQueryable<UserInfo> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<UserInfo, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, S>> orderByLambda)
        {
            var list = DB.QueryEntities<UserInfo>("Lis.User.Select.OneById").ToList();
            total = list.AsQueryable<UserInfo>().Where(whereLambda).Count();
            if (isAsc)
            {
                return list.AsQueryable<UserInfo>().Where(whereLambda)
                    .OrderBy<UserInfo, S>(orderByLambda)
                    .Skip(pageSize * (pageIndex - 1))
                    .Take(pageSize);
            }
            else
            {
                return list.AsQueryable<UserInfo>().Where(whereLambda)
                      .OrderBy<UserInfo, S>(orderByLambda)
                      .Skip(pageSize * (pageIndex - 1))
                      .Take(pageSize);
            }
        } 
        #endregion
    }
}
